import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/role_personnel_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///特殊作业-人员选择及签名(单选)
///[title] 标题
///[onCallBack] 输入回调
///[operatorType] 作业人类型 0：作业实施人 1：作业负责人 2：监护人 3:编制人 4：司索人 5:指挥人 6：用电人 7：安全交底人 8：接受交底人 9：验收人
///[hintText] 提示文字
///[isRequired] 是否必填
///[isEnabled] 是否可编辑
///[isShowDivider] 是否显示分割线
///[operator] 人员数据
///[queryParameters] 请求人员数据的参数
class CustomSignaturePage extends StatefulWidget {
  const CustomSignaturePage(
      {required this.title,
      required this.onCallBack,
      super.key,
      this.operatorType = 0,
      this.hintText,
      this.isRequired,
      this.isEnabled = true,
      this.isShowDivider = true,
      this.operator,
      this.queryParameters = const {}});

  final String title;
  final Function(ExecuteTicketReceiptListOperatorLists operatorLists)
      onCallBack;
  final int operatorType;
  final String? hintText;
  final bool? isRequired;
  final bool isEnabled;
  final bool isShowDivider;
  final ExecuteTicketReceiptListOperatorLists? operator;
  final Map<String, dynamic> queryParameters;

  @override
  State<CustomSignaturePage> createState() => _CustomSignaturePageState();
}

class _CustomSignaturePageState extends State<CustomSignaturePage> {
  ExecuteTicketReceiptListOperatorLists operator =
      ExecuteTicketReceiptListOperatorLists();

  @override
  void initState() {
    super.initState();
    if (widget.operator != null) {
      operator = widget.operator!;
    }
  }

  ///回调给外部
  void _callBack() {
    widget.onCallBack(operator);
  }

  ///获取签名样式
  Widget _getSignatureView() {
    return Container(
      alignment: Alignment.center,
      height: 36.w,
      decoration: BoxDecoration(
        color: const Color(0xffF7F7FB),
        borderRadius: BorderRadius.all(Radius.circular(4.w)),
        border: Border.all(width: 1.w, color: const Color(0xffEBEDF0)),
      ),
      child: operator.operatorUserSign!.isEmpty
          ? Text('点击此处签名',
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                color: AppColors.font_color_gray,
              ))
          : Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4.w),
                  color: Colors.white,
                  image: DecorationImage(
                    image: NetworkImage(
                        Interface.fileUrl + operator.operatorUserSign!),
                  )),
            ),
    ).clickOnce(() {
      if(!widget.isEnabled){
        return;
      }
      FocusScope.of(context).unfocus();
      //如果没有选择人员
      if (operator.operatorUserName!.isEmpty) {
        AppToast.showWarning('请先选择人员');
        return;
      }
      Get.toNamed(AppRoutes.signature)?.then((value) {
        //如果不为空则设置签名
        if (value != null) {
          setState(() {
            operator.operatorUserSign = value.toString();
          });
          _callBack();
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AppGaps.vGap20,
        Row(
          children: [
            Visibility(
                visible: widget.isRequired ?? true,
                child: Text(
                  '*',
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    color: AppColors.meaning_color_danger,
                  ),
                )),
            Text(
              widget.title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.1,
                color: AppColors.font_color_black,
              ),
            ),
            AppGaps.hGap20,
            Expanded(
              child: SizedBox(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    operator.operatorUserName!.isNotEmpty
                        ? Text(
                            operator.operatorUserName!,
                            style: TextStyle(
                              fontSize: 14.sp,
                              height: 1.2,
                              color: const Color(0xFF262F38),
                            ),
                          )
                        : Text(
                            widget.isEnabled ? widget.hintText ?? '请选择' : '暂无',
                            style: TextStyle(
                              fontSize: 14.sp,
                              height: 1.2,
                              color: AppColors.text_grey,
                            ),
                          ),
                    Visibility(
                        visible: widget.isEnabled,
                        child: Row(
                          children: [
                            AppGaps.hGap6,
                            Icon(Icons.arrow_forward_ios,
                                size: 14.sp, color: AppColors.text_grey)
                          ],
                        )),
                  ],
                ),
              ),
            ),
          ],
        ).clickOnce(() {
          FocusScope.of(context).unfocus();
          if(!widget.isEnabled){
            return;
          }
          final Map<String, dynamic> parameters = widget.queryParameters.isEmpty
              ? {'roleName': widget.title}
              : widget.queryParameters;
          Get.toNamed(AppRoutes.personnelSelection, arguments: {
            'queryParameters': parameters,
            'isRadio': true,
          })?.then((value) {
            //如果不为空则设置人员信息
            if (value != null) {
              final List<RolePersonnelEntity> personList =
                  value as List<RolePersonnelEntity>;
              final RolePersonnelEntity entity = personList[0];
              operator = ExecuteTicketReceiptListOperatorLists();
              operator.operatorUserId = entity.getId();
              operator.operatorUserName = entity.getShowName();
              operator.operatorType = widget.operatorType;
              setState(() {});
              _callBack();
            }
          });
        }),
        AppGaps.vGap10,
        _getSignatureView(),
        AppGaps.vGap20,
        Visibility(
            visible: widget.isShowDivider,
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    );
  }
}
